package com.inspur.sc.springboot.service.impl;

import com.inspur.sc.springboot.dao.SysRoleDao;
import com.inspur.sc.springboot.entity.SysRole;
import com.inspur.sc.springboot.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleDao roleDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(SysRole sysRole) {
        try {
            roleDao.save(sysRole);
            return true;
        }catch (Exception e){
            log.error(e.getMessage());
            return false;
        }
    }

    @Override
    public SysRole findSysRoleById(Integer id) {
        return roleDao.findById(id).get();
    }

    @Override
    public List<SysRole> findAll() {
        return roleDao.findAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Integer id) {
        try {
            roleDao.deleteById(id);
            return true;
        }catch (Exception e){
            log.error(e.getMessage());
            return false;
        }
    }

    @Override
    public Page<SysRole> findByNameLike(String name, Pageable pageable) {
        return roleDao.findByNameLike(name,pageable);
    }

    @Override
    public List<SysRole> findByUserId(Integer userId) {
        return roleDao.findByUserId(userId);
    }
}
